package com.hty.PowerChat;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

class ChatAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String,Object>> list_chat;
    private int[] layout;
    private String[] from;
    private int[] to;

    public ChatAdapter(Context context1, ArrayList<HashMap<String, Object>> list_chat1, int[] layout1, String[] from1, int[] to1) {
        //super();
        this.context = context1;
        this.list_chat = list_chat1;
        this.layout = layout1;
        this.from = from1;
        this.to = to1;
    }

    @Override
    public int getCount() {
        return list_chat.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /*
        View view;
        ViewHolder holder;
        int who = (Integer) list_chat.get(position).get("who");

        if(convertView == null) {
            //view = View.inflate(context, layout[who == 0 ? 0 : 1], null);
            //view = View.inflate(context, R.layout.list_chat_item_right, null);
            view = LayoutInflater.from(context).inflate(R.layout.list_chat_item_right, null);
            holder = new ViewHolder();
            holder.imageView = view.findViewById(to[who * 2]);
            holder.textView = view.findViewById(to[who * 2 + 1]);
        }else{
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        //Log.e(Thread.currentThread().getStackTrace()[2] + "", who + ", " + to[who * 2 + 0]);

        holder.imageView.setBackgroundResource((Integer)list_chat.get(position).get(from[0]));
        holder.textView.setText(list_chat.get(position).get(from[1]).toString());

        return view;
        */

        int who = (Integer) list_chat.get(position).get("who");
        LinearLayout listItem;
        if(convertView == null) {
            listItem = new LinearLayout(parent.getContext());
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //inflater.inflate(layout[who], listItem, true);
            if(who == 0){
                inflater.inflate(R.layout.list_chat_item_right, listItem, true);
            }else{
                inflater.inflate(R.layout.list_chat_item_left, listItem, true);
            }
        } else {
            listItem = (LinearLayout) convertView;
        }
        Log.e(Thread.currentThread().getStackTrace()[2] + "", convertView + ", " + listItem);
        if(who == 0){
            ImageView imageView = listItem.findViewById(R.id.imageView_avatar_me);
            TextView textView = listItem.findViewById(R.id.textView_message_me);
            imageView.setImageResource((Integer)list_chat.get(position).get(from[0]));
            textView.setText(list_chat.get(position).get(from[1]).toString());
        }else{
            ImageView imageView = listItem.findViewById(R.id.imageView_avatar_other);
            TextView textView = listItem.findViewById(R.id.textView_message_other);
            imageView.setImageResource((Integer)list_chat.get(position).get(from[0]));
            textView.setText(list_chat.get(position).get(from[1]).toString());
        }

        return listItem;
    }

}